package edu.hawaii.duedates.results;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Before; 
import org.junit.Test; 
import java.util.Calendar;

/**
 * Tests for the ResultsList class.
 * 
 * @author Arthur Shum and Erin Kim
 */
public class TestResultsList {

  private final BorrowedItem item1 = new BorrowedItem("library3", "20091111", "inserted:1");
  private final BorrowedItem item2 = new BorrowedItem("library2", "20081205", "inserted:2");
  private final BorrowedItem item3 = new BorrowedItem("library1", "20090505", "inserted:3");
  
  private BorrowedItem dueIn2Days;
  
  /**
   * Create the dueTomorrow BorrowedItem.
   */
  @Before
  public void setUp() {
    Calendar cal = Calendar.getInstance();
    // set the date to be 2 days from now
    cal.add(Calendar.DAY_OF_MONTH, 2);
    
    String yyyy = ((Integer)cal.get(Calendar.YEAR)).toString();
    // convert from 0-11 month counting to 1-12
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    // make sure they're 2 digits
    String mm = String.format("%02d", month);
    String dd = String.format("%02d", day);
    dueIn2Days = new BorrowedItem("libraryX", yyyy + mm + dd, "text");
    
  }
  
  /**
   * Test that the sort method works as expected.
   */
  @Test
  public void testSort() {
    ResultsList results = new ResultsList();
    results.add(item1);
    results.add(item2);
    results.add(item3);
    
    // check insertion order
    assertSame("1st: item1", item1, results.get(0));
    assertSame("2nd: item2", item2, results.get(1));
    assertSame("3rd: item3", item3, results.get(2));
    
    // check sort by library
    results.sort("I could put anything here");
    assertSame("1st: item3", item3, results.get(0));
    assertSame("2nd: item2", item2, results.get(1));
    assertSame("3rd: item1", item1, results.get(2));
    
    // check sort by duedate
    results.sort("duedate");
    assertSame("1st: item2", item2, results.get(0));
    assertSame("2nd: item3", item3, results.get(1));
    assertSame("3rd: item1", item1, results.get(2));
  }
  
  /**
   * Tests that the filter method works as expected.
   */
  @Test
  public void testFilter() {
    ResultsList results = new ResultsList();
    results.add(dueIn2Days);
    
    results.filter(2);
    assertTrue("Should be in the list", results.contains(dueIn2Days));
        
    results.filter(1);
    assertFalse("Should not be in the list", results.contains(dueIn2Days)); 
  }
  
  /**
   * Tests that the toString method works as expected.
   */
  @Test
  public void testToString() {
    ResultsList results = new ResultsList();
    assertEquals("No results", "=== No results found ===\n\n", results.toString());
    
    results.add(item1);
    results.add(item2);
    assertEquals("Some results", "inserted:1inserted:2", results.toString());
    
  }
  
  
}
